<?php

namespace app\common\model\banxi\sys;

use app\common\enums\MenuEndpointEnum;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * Attachment模型
 * @property string url              文件物理路径
 * @property int quote            上传(引用)次数
 * @property int last_upload_time 最后上传时间
 */
class SysRole extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_sys_role';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;
    public const ROLE_ID = 1;

    protected $append = [
        'menuEndpointCn'
    ];


    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                        ｜
    //-----------------------------------------------------------------------------------

    public function getMenuEndpointCnAttr($value, $data)
    {
        $value = $value ?: ($data['menuEndpoint'] ?? '');
        return (new MenuEndpointEnum())->getDescription($value) ?? '';
    }
    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    public function updateRole(array $role, array $menuIds)
    {
        $roleId = $role['id'];
        unset($role['roleKey']);
        $this->update($role, ['id' => $roleId]);
        $sysRoleMenuModel = new SysRoleMenu();
        $sysRoleMenuModel->where([
            'roleId' => $roleId
        ])->delete();
        return $this->saveRoleMenu($roleId, $menuIds, $role['menuEndpoint']);
    }

    public function saveRole(array $role, array $menuIds)
    {
        $role['roleSort'] = 0;
        $role['roleKey'] = createPasswordSalt();
        $this->save($role);
        $roleId = $this->id;
        return $this->saveRoleMenu($roleId, $menuIds, $role['menuEndpoint']);
    }

    private function saveRoleMenu(string $roleId, array $menuIds, string $menuEndpoint)
    {
        $roleMenus = [];
        foreach ($menuIds as $menuId) {
            $roleMenus[] = [
                'roleId' => $roleId,
                'menuId' => $menuId,
                'menuEndpoint' => $menuEndpoint
            ];
        }
        return (new SysRoleMenu())->saveAll($roleMenus);
    }

    public function removeRole($roleId)
    {
        self::destroy(function ($query) use ($roleId) {
            $query->where('id', $roleId);
        });
        $sysRoleMenuModel = new SysRoleMenu();
        return $sysRoleMenuModel->where([
            'roleId' => $roleId
        ])->delete();
    }
    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

}